<?php
/************************FoxPHP威客系统************************/
/*                                                            */
/*  FoxPHP.Com (C)2006-2016 FoxPHP Inc.                       */
/*  This is NOT a freeware, use is subject to license terms   */
/*  FoxPHP威客系统是商业软件,使用于商业用途请购买授权         */
/*  V3.0 2016.3                                               */
/*  foxphp@aliyun.com                                         */
/*  http://www.foxphp.com                                     */
/*                                                            */
/**************************************************************/

namespace Api\Controller;
use Think\Controller;
class TenpayController extends Controller {
	
	public function callbackAction(){
		$this->Callback();
	}
	
	public function Callback(){
		
		require_once (FOXPHP_PATH.'/Apps/Consume/Class/PayResponseHandler.class.php');

		/* 密钥 */
		$key = C('tenpaykey');

		if(!$key){
			$this->error('对不起!网站未开启该功能！','/');
		}

		$resHandler = new \PayResponseHandler();
		$resHandler->setKey($key);


		if($resHandler->isTenpaySign()){
		
			//交易单号
			$transaction_id = $resHandler->getParameter("transaction_id");
			
			//金额,以分为单位
			$total_fee = $resHandler->getParameter("total_fee");
			
			//支付结果
			$pay_result = $resHandler->getParameter("pay_result");
		
			if("0"==$pay_result){
				$dingdan=iconv_substr($transaction_id,10,20,C('S_CHARSET'));
				$model=new \Consume\Model\ConsumeModel();		
				$model->PayinSuccess($dingdan,$total_fee,'tenpay');
				$this->error('充值成功！','/');
			}else { 
				$this->error('验证失败！','/');
			}
		}else {
			$this->error('验证失败！','/');
		}
	}
}